package com.yanqu.road.server.logic.season.washe;

import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.activity.washe.config.WasheSeasonConfig;
import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.entity.log.LogWasheSeasonReward;
import com.yanqu.road.entity.season.SeasonRewardConfig;
import com.yanqu.road.entity.season.SeasonRewardData;
import com.yanqu.road.entity.activity.washe.data.WasheUserData;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.washe.WasheModule;
import com.yanqu.road.server.logic.season.BaseSeasonReward;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;

import java.util.Set;

public class WasheGetScoreZhanLingCondition extends BaseSeasonReward {

    private boolean inTime = false;
    private boolean newHand = true;

    public WasheGetScoreZhanLingCondition(eSeasonRewardType type, GamePlayer player) {
        super(type, player);
    }

    @Override
    protected int initSeasonId() {
        WasheConfig washeConfig = WasheMgr.getWasheConfig();
        if (washeConfig == null) {
            return WasheConfig.SEASON_ID_NULL;
        }
        WasheSeasonConfig config = washeConfig.getWasheSeasonConfig();
        if (config == null) {
            return WasheConfig.SEASON_ID_NULL;
        }
        return config.getSeasonId();
    }

    @Override
    protected int initSystemId() {
        return eSystemId.WasheActivity.getValue();
    }

    @Override
    protected boolean canChange(SeasonRewardConfig config, SeasonRewardData data, Object param) {
        long value = (long) param;
        if (value <= 0) {
            return false; // 只能加不能减
        }
        return inTime && !newHand;
    }

    @Override
    protected void changeValue(SeasonRewardConfig config, SeasonRewardData data, Object param) {
        long value = (long) param;
        data.setValue(data.getValue() + value);
    }

    @Override
    protected boolean isComplete(SeasonRewardConfig config, SeasonRewardData data) {

        // 注意：WasheMgr那边有战令自动发奖代码，改这里的话得看看那边要不要跟着改

        long check = config.getParamList().get(1);
        if (check != 0) {
            // 检查是否购买战令
            WasheUserData userData = player.getModule(WasheModule.class).getWasheUserData();
            Set<Integer> set = check == 1 ? userData.getSilverSet() : userData.getGoldSet();
            if (!set.contains(seasonId)) {
                return false;
            }
        }
        return data.getValue() >= config.getParamList().get(0);
    }

    @Override
    protected void init() {
        WasheConfig washeConfig = WasheMgr.getWasheConfig();
        if (washeConfig == null) {
            return;
        }
        WasheSeasonConfig config = washeConfig.getWasheSeasonConfig();
        if (config == null) {
            return;
        }
        long now = System.currentTimeMillis();
        if (config.getBeginTime() <= now && now < config.getEndTime()) {
            inTime = true;
        }

        WasheUserData userData = player.getModule(WasheModule.class).getWasheUserData();
        if (userData != null && userData.getDataSeason() > WasheConfig.SEASON_ID_NULL) {
            newHand = false;
        }
    }

    @Override
    protected void addLog(SeasonRewardConfig config, SeasonRewardData data) {
        long seasonPlayTimes = 0;
        long seasonPlayTime = 0;
        WasheUserData userData = player.getModule(WasheModule.class).getWasheUserData();
        if (userData != null) {
            seasonPlayTimes = userData.getPlayTimes();
            seasonPlayTime = userData.getSeasonPlayTime();
        }

        AutoLogMgr.add(new LogWasheSeasonReward(
                data.getUserId(),
                config.getType(),
                config.getSeasonId(),
                config.getRewardId(),
                seasonPlayTimes,
                seasonPlayTime
        ));
    }
}
